import { AplidActions } from '@/components/AplidCrud'

const baseConfig = {
	pageName: '用户管理',
	dictcodes: 'answer_violations',
	rowKey: 'roleId',
	addLabel: '新增用户',
	tipLabel: 'roleName',
	api: {
		index: 'sys_user_querypage',
		add: 'sys_user_add',
		edit: 'sys_user_edit',
		delete: 'sys_user_delete',
	},
	auth: {
		index: 'sys_user_querypage',
		add: 'sys_user_add',
		edit: 'sys_user_edit',
		delete: 'sys_user_delete',
	},
	menuBtn: [
		{
			icon: 'editor',
			label: '编辑',
			type: 'primary',
			operate: 'edit',
			auth: 'elderly_care_mode_edit',
			text: true,
			show: true,
		},
		{
			icon: 'detail',
			label: '详情',
			type: 'success',
			operate: 'detail',
			auth: 'elderly_care_mode_detail',
			text: true,
			show: true,
		},
		{
			icon: 'delete',
			label: '删除',
			type: 'danger',
			operate: 'delete',
			auth: 'elderly_care_mode_delete',
			text: true,
			show: true,
		},
	],
	saveFormate: async (form) => {
		return new Promise((resolve, reject) => {
			resolve(form)
		})
	},
	searchConfig: {
		inline: true,
		showBtn: true,
		labelPosition: 'left',
		column: [
			{
				label: '手机号',
				prop: 'phone',
				type: 'input',
				span: 6,
			},
			{
				label: '姓名',
				prop: 'realName',
				type: 'input',
				span: 6,
			},
		],
	},
	formConfig: {
		inline: false,
		showBtn: true,
		labelPosition: 'left',
		labelWidth: '100px',
		column: [
			{
				label: '姓名',
				prop: 'realName',
				type: 'input',
				span: 12,
			},
			{
				label: '手机号',
				prop: 'phone',
				type: 'input',
				span: 24,
			},
			{
				label: '角色',
				prop: 'roleList',
				type: 'select',
				dicData: [
					{
						label: '全部',
						value: 0,
					},
					{
						label: '本级及子级',
						value: 2,
					},
					{
						label: '本级',
						value: 3,
					},
				],
				span: 24,
			},
		],
	},
	tableConfig: {
		stripe: true,
		border: true,
		maxHeight: '80vh',
		columns: [
			{ label: '姓名', prop: 'realName' },
			{ label: '手机号', prop: 'phone' },
			{ label: '所属单位', prop: 'organizationName' },
			{
				label: '角色',
				prop: 'roleList',
				custome: 'tag',
				dicData: [
					{
						label: '全部',
						value: 0,
						type: 'success',
					},
					{
						label: '本级及子级',
						value: 2,
					},
					{
						label: '本级',
						value: 3,
					},
				],
			},
			{
				label: '账号状态',
				prop: 'lockFlag',
				custome: 'tag',
				dicData: [
					{
						label: '有效',
						value: 0,
						type: 'success',
					},
					{
						label: '锁定',
						value: 9,
						type: 'danger',
					},
				],
			},
			{
				label: '账号有效性',
				prop: 'delFlag',
				custome: 'tag',
				dicData: [
					{
						label: '正常',
						value: 0,
						type: 'success',
					},
					{
						label: '已删除',
						value: 9,
						type: 'danger',
					},
				],
			},
		],
	},
}

const Crud = defineComponent({
	emits: ['change'],
	setup(props, { slots, attrs, emit }) {
		const crud = ref(null)
		const onOperate = (type, row, title) => {
			console.log(type, row, title, 'type, row, title')
			// nextTick(() => {
			//   crud.value.handleOperate(type, row, title)
			// })
		}
		const tableConfig = {
			...baseConfig.tableConfig,
			columns: [
				...baseConfig.tableConfig.columns,
				{
					label: '操作',
					prop: 'menu',
					width: 280,
					fixed: 'right',
					render: ({ row }) => {
						const BtnList = baseConfig.menuBtn as any
						return <AplidActions list={BtnList} onChange={onOperate} row={row} />
					},
				},
			],
		}
		return () => (
			<aplid-base-crud
				ref={crud}
				api={baseConfig.api}
				addLabel={baseConfig.addLabel}
				tipLabel={baseConfig.tipLabel}
				rowKey={baseConfig.rowKey}
				pageName={baseConfig.pageName}
				auth={baseConfig.auth}
				saveFormate={baseConfig.saveFormate}
				tableConfig={tableConfig}
				searchConfig={baseConfig.searchConfig}
				operateFormate={onOperate}
			></aplid-base-crud>
		)
	},
})

export default Crud
